import type { ElMessageBoxOptions } from "element-plus";
import { ElMessageBox } from "element-plus";
import { useI18n } from "vue-i18n";

// type MsgType = "primary" | "success" | "info" | "warning" | "error";

type SubmitType = "operate" | "edit" | "unsave";

export function useMessageBox() {
  const { t } = useI18n();
  const defaultOptions: ElMessageBoxOptions = {
    confirmButtonText: t("comps.common.confirm"),
    cancelButtonText: t("comps.common.cancel"),
  };

  function alert(msg: string, options: ElMessageBoxOptions = {}) {
    return ElMessageBox.alert(msg, {
      ...defaultOptions,
      title: t("comps.messageBox.primary"),
      type: "primary",
      ...options,
    });
  }

  function confirm(msg: string, options: ElMessageBoxOptions = {}) {
    return ElMessageBox.confirm(msg, {
      title: t(`comps.messageBox.${options.type ?? "warning"}`),
      ...defaultOptions,
      type: "warning",
      ...options,
    });
  }

  function confirmDelete(msg?: string, options: ElMessageBoxOptions = {}) {
    return ElMessageBox.confirm(msg ?? t("comps.messageBox.deleteTip"), {
      ...defaultOptions,
      type: "error",
      title: t("comps.messageBox.warning"),
      confirmButtonText: t("comps.table.menu.deleteBtn"),
      confirmButtonClass: "el-button--danger",
      ...options,
    });
  }

  function submitConfirm(type: SubmitType = "operate") {
    return confirm(t(`comps.form.${type}`), { title: t("comps.messageBox.primary") });
  }
  return {
    alert,
    confirm,
    confirmDelete,
    submitConfirm,
  };
}
